package com.cty.tree;

public class ThreadedBinaryTree {
    private HeroNode root;
    //线索化的前趋结点
    private HeroNode pre = null;
    //初始化传入一个头节点
    public ThreadedBinaryTree(HeroNode root) {
        this.root = root;
    }
    //为了好看，将ThreadedNodes()重载
    public void midThreadedNodes() {
        this.midThreadedNodes(root);
    }

    //对线索化二叉树的遍历
    public void ThreadedList() {
        HeroNode node = root;
        while (node != null) {
            //首先找到中序线索化的头
            while (node.leftNodeType == 0) {
                node = node.left;
            }
            System.out.println(node);
            while (true) {
                if (node.rightNodeType == 0) {
                    break;
                }
                node = node.right;
                System.out.println(node);
            }
            node = node.right;
        }
    }

    //前序线索化二叉树
    public void preThreadedNodes(HeroNode node) {
        if (node == null) {
            return;
        }
        if (node.left == null) {
            node.left = pre;
            node.leftNodeType = 1;
        }
        if (pre != null && pre.right == null) {
            pre.right = node;
            pre.rightNodeType = 1;
        }
        pre = node;
        preThreadedNodes(node.left);
        preThreadedNodes(node.right);
    }

    //中序线索化二叉树
    public void midThreadedNodes(HeroNode node) {
        if (node == null) {
            return;
        }
        midThreadedNodes(node.left);
        if (node.left == null) {
            node.left = pre;
            node.leftNodeType = 1;
        }
        if (pre != null && pre.right == null){
            pre.right = node;
            pre.rightNodeType = 1;
        }
        pre = node;
        midThreadedNodes(node.right);
    }

    //前序遍历
    public void preOrder() {
        if (root == null) {
            System.out.println("二叉树为空~~");
            return;
        } else {
            root.proOrder();
        }
    }

    //中序遍历
    public void midOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
            return;
        } else {
            root.midOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        if (root == null) {
            System.out.println("空二叉树~~");
            return;
        } else {
            root.postOrder();
        }
    }

    //前序查找
    public HeroNode preOrderSearch(int no) {
        HeroNode resNode = null;
        resNode = root.preOrderSearch(no);
        if (resNode != null) {
            return resNode;
        } else {
            return null;
        }
    }

    //中序查找
    public HeroNode midOrderSearch(int no) {
        HeroNode resNode = null;
        resNode = root.midOrderSearch(no);
        if (resNode != null) {
            return resNode;
        } else {
            return null;
        }
    }

    //后序查找
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        resNode = root.postOrderSearch(no);
        if (resNode != null) {
            return resNode;
        } else {
            return null;
        }
    }

    //删除节点
    public void delNode(int no) {
        if (root == null) {
            System.out.println("二叉树为空~~");
            return;
        }
        if (root.id == no) {
            root = null;
            return;
        } else {
            root.delNode(no);
        }
    }
}
